/*
 * File: HibernateUtil.java
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package tvseries.utils;

import java.util.List;

import org.hibernate.ObjectNotFoundException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

/**
 * This is a utility class that provides a metod for acquiring a Hibernate Session.
 * 
 * @author nish.naidoo@gmail.com
 * 
 */
public class HibernateUtil
{
	private static final SessionFactory sessionFactory;

	/**
	 * This block is called when the class is first loaded, therefore executed only once. This is a Java
	 * mechanism for creating a singleton.
	 */
	static
	{
		try
		{
			// Create the SessionFactory from hibernate.cfg.xml
			sessionFactory = new Configuration().configure().buildSessionFactory();
		}
		catch (Throwable ex)
		{
			// Make sure you log the exception, as it might be swallowed
			System.err.println("Initial SessionFactory creation failed." + ex);
			throw new ExceptionInInitializerError(ex);
		}
	}

	/**
	 * @return the SessionFactory instance
	 */
	public static SessionFactory getSessionFactory()
	{
		return sessionFactory;
	}

	/**
	 * Persist the entity to the data store.
	 * 
	 * @param entity the entity to persist.
	 */
	public static void persist(Object entity)
	{
		Session session = getSessionFactory().getCurrentSession();
		Transaction tx = session.beginTransaction();
		session.saveOrUpdate(entity);
		tx.commit();
	}

	/**
	 * Load an entity from the data store.
	 * 
	 * @param c Class of entity to load.
	 * @param id id of entity the load.
	 * @return the loaded object.
	 * 
	 * @throws ObjectNotFoundException
	 */
	public static Object load(Class<?> c, String id) throws ObjectNotFoundException
	{
		Session session = getSessionFactory().getCurrentSession();
		Transaction tx = session.beginTransaction();
		Object o = session.load(c, id);
		tx.commit();
		
		return o;
	}
	
	/**
	 * Delete the object from the data store.
	 * 
	 * @param entity the entity to delete.
	 */
	public static void delete(Object entity)
	{
		Session session = getSessionFactory().getCurrentSession();
		Transaction tx = session.beginTransaction();
		session.delete(entity);
		tx.commit();
	}
	
	/**
	 * Get all objects of a particular class from the data store.
	 * 
	 * @param c class of the objects to be retrieved.
	 * @return list of objects of the given class.
	 */
	public static List<?> getObjects(Class<?> c)
	{
		Session session = getSessionFactory().getCurrentSession();
		Transaction tx = session.beginTransaction();
		List<?> result = session.createQuery("from " + c.getSimpleName() + " order by id").list();
		tx.commit();
		
		return result;
	}
}
